package com.fadadianqi.logistics.admin.filter;

import com.fadadianqi.logistics.admin.staff.model.Master;
import com.fadadianqi.logistics.admin.staff.service.ActionEnum;
import com.fadadianqi.logistics.admin.staff.service.MasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class URIInspector {

    private Master master;

    private HashMap<String, Boolean> permissionURI = new HashMap<String, Boolean>();

    public void setMaster(Master master) {
        this.master = master;
    }

    public HashMap<String, Boolean> getPermissionURI() {
        return permissionURI;
    }

    public URIInspector() {
    }

    private void permissionBuf() {
        permissionURI.put("/a/store/list", masterService.hasPermission(master, ActionEnum.STORE_VIEW));
        permissionURI.put("/a/store/form", masterService.hasPermission(master, ActionEnum.STORE_MODIFY));

        permissionURI.put("/a/store/backtrack", masterService.hasPermission(master, ActionEnum.STORE_BACKTRACK));
        permissionURI.put("/a/store/backtrack/form", masterService.hasPermission(master, ActionEnum.STORE_BACKTRACK));

        permissionURI.put("/a/store/house", masterService.hasPermission(master, ActionEnum.STORE_HOUSE_VIEW));
        permissionURI.put("/a/batch/list", masterService.hasPermission(master, ActionEnum.STORE_VIEW_BATCH));
        permissionURI.put("/a/sell/form", masterService.hasPermission(master, ActionEnum.SELL_MODIFY));
        permissionURI.put("/a/sell/list", masterService.hasPermission(master, ActionEnum.SELL_VIEW));
        permissionURI.put("/a/customer/list", masterService.hasPermission(master, ActionEnum.CUSTOMER_VIEW));
        permissionURI.put("/a/customer/form", masterService.hasPermission(master, ActionEnum.CUSTOMER_MODIFY));
        permissionURI.put("/a/stat", masterService.hasPermission(master, ActionEnum.STAT_VIEW));

        Boolean bv = masterService.hasPermission(master, ActionEnum.BRAND_VIEW);
        Boolean cv = masterService.hasPermission(master, ActionEnum.CATEGORY_VIEW);
        permissionURI.put("/a/brand-category/list", bv & cv);

        permissionURI.put("/a/brand/list", bv);
        permissionURI.put("/a/category/list", cv);

        permissionURI.put("/a/goods/list", masterService.hasPermission(master, ActionEnum.GOODS_VIEW));
        permissionURI.put("/a/in-out/list", masterService.hasPermission(master, ActionEnum.IN_OUT_VIEW));

        permissionURI.put("/a/village/list", masterService.hasPermission(master, ActionEnum.AREA_MODIFY));
    }

    public boolean hasPermission(String uri) {
        permissionBuf();
        Boolean b = permissionURI.get(uri);
        if (b == null) {
            b = Boolean.TRUE;
        }
        return b;
    }

    @Autowired
    private MasterService masterService;
}
